﻿using Microsoft.Xna.Framework;
using OTAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.ID;
using TShockAPI;

namespace TShock_AI
{
    public class Projectile_AI
    {
        public static Projectile_AI[] Projectile_Data = new Projectile_AI[Main.projectile.Length];
        public static Random random = new Random();
        public Projectile projectile;
        public NPC npc;
        public Projectile_AI(Projectile projectile)
        {
            this.projectile = projectile;
        }

        public Projectile_AI(Projectile projectile, NPC npc) : this(projectile)
        {
            this.npc = npc;
        }

        public virtual HookResult OnPreAI()
        {
            return HookResult.Continue;
        }

        public virtual void OnPostAI()
        {
        }

        public virtual void OnAttackedPlayer(TSPlayer player, int damage)
        {
            NPC_AI.NPC_Data[npc.whoAmI].OnAttackedPlayer(player, damage);
        }

        public virtual void OnPostKilled()
        {
        }

        public Projectile NewProjectile(Vector2 position, Vector2 velocity, int type, int damage, float knockBack, int owner = 255, float ai0 = 0.0f, float ai1 = 0.0f)
        {
            Projectile projectile = Main.projectile[Projectile.NewProjectile(npc.GetSpawnSourceForNPCFromNPCAI(), position, velocity, type, damage, knockBack, owner, ai0, ai1)];
            return projectile;
        }
    }
    public class SandnadoHostile : Projectile_AI
    {
        public SandnadoHostile(Projectile projectile, NPC npc) : base(projectile, npc)
        {
        }

        public override HookResult OnPreAI()
        {
            projectile.position.X += 0.1f;
            projectile.netUpdate = true;
            return base.OnPreAI();
        }

        public override void OnPostAI()
        {

        }

        public override void OnPostKilled()
        {
            int value = random.Next(0, 3);
            if (value == 0)
            {
                NewProjectile(projectile.Center + Vector2.UnitY * 16, Vector2.Zero, ProjectileID.VortexVortexLightning, 0, 32);
            }
            else if (value == 1)
            {
                for (int i = 0; i < 16; i++)
                {
                    NewProjectile(projectile.Top, Vector2.UnitY.RotatedBy(2 * Math.PI / 16 * i) * 16, ProjectileID.MartianTurretBolt, 20, 32);
                }
            }
            else if (value == 2)
            {
                for (int i = 0; i < 16; i++)
                {
                    NewProjectile(projectile.Top, Vector2.UnitY.RotatedBy(2 * Math.PI / 16 * i) * 10, ProjectileID.BrainScramblerBolt, 20, 32);
                }
            }
        }
    }
}

